import api from "../../common/api";
import p from "../../utils/platform";

const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    offlineName: {
      type: String,
      value: ""
    },
    offlineId: {
      type: String,
      value: ""
    },
      flag: {
      type: String,
      value: ""
    },
    offlineReason: {
      type: String,
      value: ""
    },
  },

  options: {
    styleIsolation: 'apply-shared',
    addGlobalClass: true,
  },

  /**
   * 组件的初始数据
   */
  data: {
    state: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleOpen() {
      app.globalData.offlineRedPaperFlag = 2
      console.log(this.data)
      let that = this;
      if(this.data.offlineId==''){
        if(this.data.offlineReason=='该活动只针对新用户哦，您已是树懒老用户啦~'){
          this.setData({
            state: 2
          })
        }else {
          this.setData({
            state: 3
          })
        }
      } else {
        api.openRedpacket({
          userId: this.data.offlineId
          // userId: 'ac46619165bf47fd981a43c15f452326'
        }, res => {
            this.setData({
              state: 1,
              num: res.data.content.amount,
            })
        })
      }
    },

    handleGo() {
      this.triggerEvent("onredpaperclose")
    },

    handleClose() {
      app.globalData.offlineRedPaperFlag = 2
      if(this.data.state==0){
        p.showModal({
          content: "您还未领取红包，是否确定关闭。",
          cancelText: "取消",
          confirmText: "关闭"
        })
        .then((res) => {
          if (res.confirm) {
            this.handleGo()
          } 
        })
      }else {
        this.handleGo()
      }
    },
  }
})